{ Statement: http://informatics.mccme.ru/moodle/mod/statements/view3.php?chapterid=185
  Verdict: Accepted
}
uses
	SysUtils, Math; 
 
type
	al = array[1..3] of longint;
 
var
	i, n, m, x, y, w, cc, ans, a, b, cur, kk : longint;
	p : array[0..100010] of longint;
	g : array[0..100010] of al;
	res : array[0..100010, 1..2] of longint;
 
procedure put(x, y, w : longint);
begin
	inc(cc);
	g[cc][1] := x;
	g[cc][2] := y;
	g[cc][3] := w;
end;
 
procedure sort(l, r : longint);
 
var
	i, j, x : longint;
	y : al;
 
begin
	i := l;
	j := r;
	x := g[(l + r) shr 1][3];
	repeat
		while g[i][3] < x do inc(i);
		while g[j][3] > x do dec(j);
		if i <= j then begin
			y := g[i];
			g[i] := g[j];
			g[j] := y;
			inc(i);
			dec(j);
		end;
	until i > j;
	if l < j then sort(l, j);
	if i < r then sort(i, r);
end;
 
function dsuGet(v : longint) : longint;
begin
	if v = p[v] then result := v else begin
		p[v] := dsuGet(p[v]);
		result := p[v];
	end;
end;
 
procedure dsuUnite(a, b : longint);
 
var
	t : longint;
 
begin
	a := dsuGet(a);
	b := dsuGet(b);
	if odd(random(2)) then begin
		t := a;
		a := b;
		b := t;
	end;
	if a <> b then p[a] := b;
end;
 
begin
	reset(input,'input.txt');
	rewrite(output,'output.txt');
	read(n, m);
	cc := 0;
	for i := 1 to m do begin
		read(x, y, w);
		put(x, y, w);
	end;
	ans := 0;
	sort(1, cc);
	for i := 1 to n do
		p[i] := i;
	kk := 0;
	for i := 1 to m do begin
		a := g[i][1];
		b := g[i][2];
		cur := g[i][3];
		if dsuGet(a) <> dsuGet(b) then begin
			inc(ans, cur);
			inc(kk);
			res[kk][1] := a;
			res[kk][2] := b;
			dsuUnite(a, b);
		end;
	end;
	write(ans);
end.